#ifndef AICODER_H
#define AICODER_H

#include <cbplugin.h>
#include <settings.h>
#include <editormanager.h>
#include <projectmanager.h>
#include <cbeditor.h>      // 添加cbeditor头文件以获取cbStyledTextCtrl
#include <editorbase.h>    // 添加editorbase头文件
#include <wx/string.h>
#include <wx/menu.h>
#include <wx/menuitem.h>
#include <wx/frame.h>
#include <memory>
#include <map>
#include <thread>

// 包含协作框架头文件
#include "CollaborativeCore/CollaborationProtocol.h"
#include "CollaborativeCore/RealTimeEditor.h"
#include "ModelModuleSystem/ModelComponent.h"
#include "GlobalRegistry/ComponentRegistry.h"

class AICoder : public cbPlugin
{
public:
    AICoder();
    ~AICoder() override;

    void OnAttach() override;
    void OnRelease(bool appShutDown) override;
    void BuildMenu(wxMenuBar* menuBar) override;
    void BuildModuleMenu(const ModuleType type, wxMenu* menu, const FileTreeData* data = nullptr) override;

    // 现有核心功能
    void GenerateCode();
    void RefactorCurrentFile();
    void AnalyzeProject();
    void SelfRefactorUEStudio();

    // 新增协作功能
    void JoinCollaborationSession();
    void CreateModelComponent();
    void PublishComponent();
    void ShowComponentBrowser();
    void StartCollaborationServer();

private:
    // 现有事件处理
    void OnAIGenerateCode(wxCommandEvent& event);
    void OnAIRefactor(wxCommandEvent& event);
    void OnAIAnalyze(wxCommandEvent& event);
    void OnSelfRefactor(wxCommandEvent& event);
    
    // 新增协作事件处理
    void OnJoinCollaboration(wxCommandEvent& event);
    void OnCreateComponent(wxCommandEvent& event);
    void OnPublishComponent(wxCommandEvent& event);
    void OnShowComponentBrowser(wxCommandEvent& event);

    // 现有辅助方法
    wxString CallDeepSeekAPI(const wxString& prompt);
    wxString GetCurrentFileContent();
    wxString GetProjectStructure();

    // 新增协作辅助方法
    void InitializeCollaborationFramework();
    void SetupCollaborationCallbacks();
    void HandleIncomingCollaboration(const CollaborationMessage& message);
    void UpdateCollaborationUI(const wxString& status);
    wxString GetCurrentDeveloperId();
    void CollaborationThread();
    void StopCollaboration();

    // 成员变量
    wxMenu* m_aiMenu;
    wxMenu* m_collaborationMenu;

    // 新增协作成员
    std::unique_ptr<CRealTimeEditor> m_realTimeEditor;
    std::map<wxString, std::shared_ptr<CModelComponent>> m_localComponents;
    
    std::thread m_collaborationThread;
    bool m_collaborationRunning;
    bool m_isInCollaborationSession;

    wxDECLARE_EVENT_TABLE();
};

#endif
